<?php
/* 
 *待发货管理
 */
class ToshippingAction extends TransAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Product.name'] = array('like',"%".$key."%");
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
            $where['Sale.po'] = array('like',"%".$key."%");
            $where['Order_detail.mano'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }

        if(!empty($_REQUEST['customer']))
            $map['_string'] = "(Customer.name like '%".trim($_REQUEST['customer'])."%') or (Customer.company like '%".trim($_REQUEST['customer'])."%')";
        if( !empty($_REQUEST['customer_id']) ){
            $map['Customer.id'] = trim($_REQUEST['customer_id']);
        }

        if($_REQUEST['dstatus'] != '' )
            $map['Order_detail.status'] =  $_REQUEST['dstatus'];
        else
            $map['Order_detail.status'] = array('between',array('0','4')); 


         //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': //交货期
                $map['Order_detail.delivery_date '] = $cond_time;
                break;
              case '2': //创建时间
                $map['Order_detail.create_time '] = $cond_time;
                break;
              
              default:  //按下单时间查询
                $map['Order_detail.create_time '] = $cond_time;
                break;
            }
            
        }

        
    }

    //待发货列表
    public function index(){
        parent::index('Order_detailView');
    }

    public function update(){
        A('Order_detail')->update();
    }

    //某个待发货的小订单详情
    public function odetail(){
        $model = M ( 'Order_detail' );
        $id = $_REQUEST ['id'];
        $vo = $model->getById ( $id );
        $this->assign ( 'vo', $vo );

        $stocks = M('Stock')->where('product_id ='.$vo['product_id'])->findAll();
        $this->assign('stocks',$stocks);
        $this->display ();
    }

    //发布采购任务页面
    public function open_buy_task()
    {
        $this->display();
    }


    //发布采购任务
    public function pub_buy_task()
    {   A('Buy')->_before_insert();
        A('Buy')->insert();
    }


    //打包操作，状态必须是“仓库操作中”且是同一个客户的订单，才能打包
    public function doshipping(){
        $model = M ( 'Order_detail');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $condition = array (
                'id' => array ('in', explode ( ',', $id ) ),
                'status' =>4 ,//仓库操作中
             );
            $vo = $model->where($condition)->findAll();
            if(!is_array($vo))
                $this->error('请选择状态为 仓库操作中 的销售订单进行打包！');
            $cid1 = 0;  //customer_id
            foreach ($vo as $key => $value) {
                if($key == 0)
                    $cid1 = getModelName($value['order_id'],'Order','customer_id');
                else{
                    if($cid1 == getModelName($value['order_id'],'Order','customer_id'))
                        continue;
                    else
                        $this->error('请选择同一个客户的销售订单进行打包！');
                }

            }
            $this->assign ( 'vo', $vo );
            $this->display ();
        }else{
            $this->error('请选择待发货的销售单号！');
        }
        
    }

    //仓库操作中
    public function prepair()
    {
        $order_detail_id = $_REQUEST['id'];
        //仓库操作中
        $order_detail = M('Order_detail')->where('id = '.$order_detail_id)->field('product_id,specification,status')->find();
        if(!$rs = M('Stock')->where('product_id = '.$order_detail['product_id'].' and specification = '.$order_detail['specification'])->find()  ){
	  $this->error("库存中尚未有相同规格的产品，不能操作!",false,5);
	}	
        $sid = M('Order_detail')->where('id = '.$order_detail_id)->setField('status',4);
        if($sid){
            A('Action')->writeLog("状态改为仓库操作中",'修改','Order_detail',$order_detail_id,$order_detail['status'],4);
            $jumpUrl = Cookie::get ( '_currentUrl_' );//返回上一次操作页面
             $this->assign('jumpUrl',$jumpUrl);
            $this->success("状态成功改为：仓库操作中！"); 
        }else
            $this->error('操作失败！');
         
    }


}
?>
